package com.java.jiuyang.commons.controller;

/**
 * @ClassName: GetGaoDeMapController
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/26 0026 上午 11:33
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/26 0026        Fu Hao      v1.0.0         创建
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java.jiuyang.commons.dao.TGdMapMapper;
import com.java.jiuyang.commons.entity.TGdMap;
import com.java.jiuyang.commons.utils.GaodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Controller
@RequestMapping("/getAll")
public class GetGaoDeMapController {

    @Resource
    private TGdMapMapper tGdMapMapper;
    @RequestMapping("/getSql")
    public Object getRegionList(){
        String subdistrict = "4"; // 3 表示 中国往下查3级
        String address = "中国";
        //获取高德API查询到的信息
        String geoInfo = GaodeUtil.getProvinceInfo(address,subdistrict);
        //把获取的信息转换成json对象
        JSONObject jsonObject = JSONObject.parseObject(geoInfo);
        //获取到省及下面所有的信息 开始循环插入
        JSONArray countryAll=jsonObject.getJSONArray("districts");
        for (int i=0;i<countryAll.size();i++) {
            JSONObject countryLeve0 = countryAll.getJSONObject(i);
            String citycode0 = countryLeve0.getString("citycode");
            String adcode0 = countryLeve0.getString("adcode");
            String name0 = countryLeve0.getString("name");
            String center0 = countryLeve0.getString("center");
            String longitude0=null;
            String latitude0=null;
            if (center0.contains(",")){
                int index=center0.indexOf(",");
                //获取经度
                longitude0=center0.substring(0,index);
                //获取纬度
                latitude0=center0.substring(index+1);
            }
            String country = countryLeve0.getString("level");
            String level = null;
            if (country.equals("country")) {
                level = "country";
            }
            //插入国家
            String id1 = insert(null, adcode0, citycode0, name0,longitude0,latitude0, level, name0);
            JSONArray province0 = countryLeve0.getJSONArray("districts");

            for (int j = 0; j < province0.size(); j++) {
                JSONObject province1 = province0.getJSONObject(j);
                String citycode1 = province1.getString("citycode");
                String adcode1 = province1.getString("adcode");
                String name1 = province1.getString("name");
                String center1 = province1.getString("center");
                String longitude1=null;
                String latitude1=null;
                if (center1.contains(",")){
                    int index=center1.indexOf(",");
                    //获取经度
                    longitude1=center1.substring(0,index);
                    //获取纬度
                    latitude1=center1.substring(index+1);
                }
                String province = province1.getString("level");
                String level1 = null;
                if (province.equals("province")) {
                    level1 = "province";
                }
                //删除香港台湾澳门
                if (Objects.equals("香港特别行政区",name1)||Objects.equals("台湾省",name1)||Objects.equals("澳门特别行政区",name1)){
                    continue;
                }
                //插入省
                String id2 = insert(id1, adcode1, citycode1, name1, longitude1,latitude1, level1, name0, name1);
                JSONArray city0 = province1.getJSONArray("districts");
                for (int z = 0; z < city0.size(); z++) {
                    JSONObject city2 = city0.getJSONObject(z);
                    String citycode2 = city2.getString("citycode");
                    String adcode2 = city2.getString("adcode");
                    String name2 = city2.getString("name");
                    String center2 = city2.getString("center");
                    String longitude2=null;
                    String latitude2=null;
                    if (center2.contains(",")){
                        int index=center2.indexOf(",");
                        //获取经度
                        longitude2=center2.substring(0,index);
                        //获取纬度
                        latitude2=center2.substring(index+1);
                    }
                    String city = city2.getString("level");
                    String level2 = null;
                    if (city.equals("city")) {
                        level2 = "city";
                    }
                    //插入市
                    String id3 = insert(id2, adcode2, citycode2, name2, longitude2,latitude2, level2, name0, name1, name2);
                    JSONArray street0 = city2.getJSONArray("districts");
                    for (int w = 0; w < street0.size(); w++) {
                        JSONObject street3 = street0.getJSONObject(w);
                        String citycode3 = street3.getString("citycode");
                        String adcode3 = street3.getString("adcode");
                        String name3 = street3.getString("name");
                        String center3 = street3.getString("center");
                        String longitude3=null;
                        String latitude3=null;
                        if (center3.contains(",")){
                            int index=center3.indexOf(",");
                            //获取经度
                            longitude3=center3.substring(0,index);
                            //获取纬度
                            latitude3=center3.substring(index+1);
                        }
                        String street = street3.getString("level");
                        String level3 = null;
                        if (street.equals("district")) {
                            level3 = "district";
                        }
                        if (street.equals("street")) {
                            level3 = "street";
                        }
                        //插入区县
                        insert(id3, adcode3, citycode3, name3, longitude3,latitude3, level3, name0, name1, name2, name3);
                    }
                }
            }
        }
        return "ok";
    }


    /**
     * 数据入库
     *
     * @param
     * @param citycode  城市编码
     * @param adcode    区域编码  街道没有独有的adcode，均继承父类（区县）的adcode
     * @param name      城市名称 （行政区名称）
     * @param center    地理坐标
     * @param level     等级  国家，省，市....
     * @param mergeName 把名称拼接成全名
     * @return adCode  做为parentadCode
     */
    public String insert(String parentAdCode, String adcode,String citycode,String name,String longitude,String latitude,String level, String... mergeName) {
        System.out.println("*********************开始录入数据***********************");
        TGdMap record = new TGdMap();
        if (!Objects.equals(adcode,"[]")) {
            record.setAdcode(adcode);
        }
        if (!Objects.equals(citycode,"[]")) {
            record.setCitycode(citycode);
        }
        record.setLongitude(longitude);
        record.setLatitude(latitude);
        record.setLevel(level);
        record.setName(name);
        record.setParentAdcode(parentAdCode);
        record.setUpdateTime(new Date());
        String megName = "";
        for (int i = 0; i < mergeName.length; i++) {
            megName = megName + mergeName[i];
            if (i < mergeName.length - 1) {
                megName = megName + ",";
            }
        }
        record.setMergerName(megName);
        tGdMapMapper.insertSelective(record);
        System.out.println("------------插入完成------------");
        return  record.getAdcode();
    }

}
